﻿package com.Galaxisfarm {
	// Bibliotecas da game engine Flixel
	import com.adamatomic.flixel.*;
	// Utilitários
	import flash.utils.*;
	
	public class Hud {
		// Sprite da hud
		[Embed(source = "../../data/hud.png")] private var ImgHud:Class;
		// Sprite do item da árvore
		[Embed(source = "../../data/hud_tree.png")] private var ImgHudTree:Class;
		// Sprite do ponteiro do relógio
		[Embed(source = "../../data/hud_pointer.png")] private var ImgHudPointer:Class;
		// Sprite da carne
		[Embed(source = "../../data/meat_hud.png")] private var ImgHudMeat:Class;
		// Sprite da esterco
		[Embed(source = "../../data/manure_hud.png")] private var ImgHudManure:Class;
		// Sprite da leiteira
		[Embed(source = "../../data/dairy_hud.png")] private var ImgHudDairy:Class;
		// Sprite do medicamento
		[Embed(source = "../../data/drug_hud.png")] private var ImgHudDrug:Class;
		// Sprite da vaca morta
		[Embed(source = "../../data/dead_cow_hud.png")] private var ImgHudDeadCow:Class;
		// Fonte da hud
		[Embed(source='../../data/calist.ttf', fontName="fontCalist", mimeType="application/x-font-truetype")] private var fontCalist:Class;
		
		// Hud
		private var _hud:FlxSprite;
		// Hud ponteiro
		private var _hud_pointer:FlxSprite;
		// Hud item de árvore
		private var _hud_tree:FlxSprite;
		// Mensagem de dinheiro
		private var _message_money:FlxText;
		// Mensagem de tempo
		private var _message_time:FlxText;
		// Mensagem da reserva legal
		private var _message_legal_reserve:FlxText;
		// Segundos por minuto
		private var _seconds_per_minute:int = 60;
		// Minutos por hora
		private var _minutes_per_hour:int = 60;
		// Quantidade de elementos
		private var _items:FlxArray;
		
		// Método construtor
		public function Hud(X:Number, Y:Number):void {
			_message_legal_reserve = null;
			_hud_tree = null;
			
			_hud = new FlxSprite(ImgHud, X, Y);
			GameState.getLyrSprites().add(_hud);
			
			_hud_pointer = new FlxSprite(ImgHudPointer, _hud.x + 179, _hud.y + 7);
			GameState.getLyrSprites().add(_hud_pointer);
			
			_message_money = new FlxText(_hud.x + 41, _hud.y + 7, 60, 16, String(Farm.getMoney()), 0xffffffff, "fontCalist", 14, "center");
			GameState.getLyrSprites().add(_message_money);
			
			_message_time = new FlxText(_hud.x + 200, _hud.y + 7, 58, 16, convertSecondsToHHMMSS(Farm.getTime()), 0xffffffff, "fontCalist", 14, "center");
			GameState.getLyrSprites().add(_message_time);
			
			_items = new FlxArray();
		}
		
		// Adiciona item
		public function addItem(Item:String):void {
			if (_items.length < 2) {
				var obj:* = null;
				switch(Item) {
					case "com.Galaxisfarm::Meat":
						obj = new FlxSprite(ImgHudMeat, (_hud.x + 114) + (24 * _items.length), _hud.y + 10, false, false, 24, 15);
						break;
					case "com.Galaxisfarm::Manure":
						obj = new FlxSprite(ImgHudManure, (_hud.x + 114) + (24 * _items.length), _hud.y + 10, false, false, 24, 15);
						break;
					case "com.Galaxisfarm::Dairy":
						obj = new FlxSprite(ImgHudDairy, (_hud.x + 114) + (24 * _items.length), _hud.y + 10, false, false, 24, 15);
						break;
					case "com.Galaxisfarm::Drug":
						obj = new FlxSprite(ImgHudDrug, (_hud.x + 114) + (24 * _items.length), _hud.y + 10, false, false, 24, 15);
						break;
					case "com.Galaxisfarm::DeadCow":
						obj = new FlxSprite(ImgHudDeadCow, (_hud.x + 114) + (24 * _items.length), _hud.y + 10, false, false, 24, 20);
						break;
				}
				
				if (obj != null) {
					GameState.getLyrSprites().add(obj);
					_items.add(obj);
				}
			}
		}
		
		// Arruma a posição dos items
		private function arrangePositionItems():void {
			for (var i:int = 0; i < _items.length; i++) {
				_items[0].x = (_hud.x + 114) + (_items[0].width * i);
			}
		}
		
		// Remove item
		public function removeItem():void {			
			if (_items.length > 0) {
				_items[0].kill();
				_items.removeAt(0, true);
				arrangePositionItems();
			}
		}
		
		// Define a reserva legal
		public function setLegalReserve(legalReserve:*):void {
			if (_message_legal_reserve == null)
				setMessageLegalReserve();
			_message_legal_reserve.setText(legalReserve + "%");
		}
		
		// Cria o texto da mensagem da reserva legal
		public function setMessageLegalReserve():void {
			if (_message_legal_reserve == null) {
				_message_legal_reserve = new FlxText(_hud.x + 117, _hud.y + 7, 44, 16, "100%", 0xffffffff, "fontCalist", 14, "right");
				GameState.getLyrSprites().add(_message_legal_reserve);
				
				_hud_tree = new FlxSprite(ImgHudTree, _hud.x + 111, _hud.y + 8);
				GameState.getLyrSprites().add(_hud_tree);
			}
		}
		
		// Define o dinheiro
		public function setMoney(money:*):void {
			_message_money.setText(String(money));
			
			// Indica em cores se foi ganho, perdido ou mantido
			// o dinheiro
			if (Farm.getMoney() > Farm.getLastMoney()) {
				_message_money.setColor(0xff00ff00);
			} else if (Farm.getMoney() < Farm.getLastMoney()) {
				_message_money.setColor(0xffff0000);
			} else {
				_message_money.setColor(0xffffffff);
			}
		}
		
		// Define o tempo
		public function setTime(t:*):void {
			_message_time.setText(convertSecondsToHHMMSS(t));
		}
		
		// Para o relógio
		public function stopClock():void {
			_hud_pointer.angularVelocity = 0;
		}
		
		// Roda o relógio
		public function runClock():void {
			if (_hud_pointer.angularVelocity == 0)
				_hud_pointer.angularVelocity = 6;
		}
		
		// Converte segundos no formato HH:MM:SS
		private function convertSecondsToHHMMSS(intSecondsToConvert:Number):String {
			var hours:String = String(convertHours(intSecondsToConvert));
			var minutes:String = String(getRemainingMinutes(intSecondsToConvert));
			minutes = (int(minutes) == 60) ? "00" : minutes;
			var seconds:String = String(getRemainingSeconds(intSecondsToConvert));
			
			if (int(hours) < 10) hours = "0" + hours;
			if (int(minutes) < 10) minutes = "0" + minutes;
			if (int(seconds) < 10) seconds = "0" + seconds;
			
			var formated:String = null;
			if (int(hours) > 0)
				formated = hours + ":" + minutes + ":" + seconds;
			else
				formated = minutes + ":" + seconds;
			
			return formated;
		}
		
		// Converte as horas
		private function convertHours(intSeconds:Number):int {
			var minutes:int = convertMinutes(intSeconds);
			var hours:int = Math.floor(minutes / _minutes_per_hour);
			return hours;
		}
		
		// Converte os minutos
		private function convertMinutes(intSeconds:Number):int {
			return Math.floor(intSeconds / _seconds_per_minute);
		}
		
		// Obtém os segundos restantes
		private function getRemainingSeconds(intTotalSeconds:Number):int {
			return (intTotalSeconds % _seconds_per_minute);
		}
		
		// Obtém os minutos restantes
		private function getRemainingMinutes(intSeconds:Number):int {
			var intTotalMinutes:int = convertMinutes(intSeconds);
			return (intTotalMinutes % _minutes_per_hour);
		}
	}
}